package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.ErrorConstants;
import com.ruoyi.common.core.domain.BaseDto;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.dao.PmsArchivesDao;
import com.ruoyi.system.domain.dto.PmsArchivesDto;
import com.ruoyi.system.domain.po.*;
import com.ruoyi.system.domain.vo.PmsArchivesVo;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.PmsArchivesService;
import com.ruoyi.system.service.TCodeServier;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.ruoyi.common.constant.UserConstants.QuestionCode.*;


/**
 * @author nanyang
 */
@Slf4j
@Service
public class PmsArchivesServiceImpl implements PmsArchivesService {

    @Resource
    private PmsArchivesMapper pmsArchivesMapper;

    @Resource
    private PmsArchivesDao pmsArchivesDao;

    @Resource
    private PmsArchivesRecordMapper pmsArchivesRecordMapper;

    @Resource
    private CmsArchivesCodeTagMapper cmsArchivesCodeTagMapper;
    @Resource
    private TCodeServier codeServier;
    @Resource
    private BlsZlInfoRecordMapper blsZlInfoRecordMapper;
    @Resource
    private PmsPatientMapper pmsPatientMapper;
    @Resource
    private BlsZlInfoMapper blsZlInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertPmsArchives(PmsArchivesDto.Insert pmsArchivesInsert) {
        // 检查身份证号是否重复
        PmsArchivesExample pmsArchivesExample = new PmsArchivesExample();
        pmsArchivesExample.createCriteria().andDelFlagEqualTo((byte) 0).andIdCardEqualTo(pmsArchivesInsert.getIdCard());
        List<PmsArchives> pmsArchivesList = pmsArchivesMapper.selectByExample(pmsArchivesExample);
        if (!pmsArchivesList.isEmpty()) {
            throw new BaseException(ErrorConstants.DUPLICATE_ID_CARD);
        }
        // 新增实体数据
        long pmsArchivesId = IdUtil.getSnowflake().nextId();
        PmsArchives pmsArchives = BeanUtil.copyProperties(pmsArchivesInsert, PmsArchives.class);
        pmsArchives.setId(pmsArchivesId);
        pmsArchives.setPinyinFull(PinyinUtil.getPinyin(pmsArchives.getName()));
        pmsArchives.setPinyinSimple(PinyinUtil.getFirstLetter(pmsArchives.getName(), ""));
        pmsArchives.setCreateBy(SecurityUtils.getUsername());
        pmsArchives.setCreateTime(new Date());
        int i = pmsArchivesMapper.insertSelective(pmsArchives);
        // 新增子表数据
        List<PmsArchivesDto.ArchivesRecordDto> recordDtoList = pmsArchivesInsert.getRecordDtoList();
        if (i > 0 && !recordDtoList.isEmpty()) {
            recordDtoList.forEach(recordDto -> {
                PmsArchivesRecord pmsArchivesRecord = BeanUtil.copyProperties(recordDto, PmsArchivesRecord.class);
                pmsArchivesRecord.setId(IdUtil.getSnowflake().nextId());
                pmsArchivesRecord.setArchivesId(pmsArchivesId);
                pmsArchivesRecord.setCreatedAt(new Date());
                pmsArchivesRecordMapper.insertSelective(pmsArchivesRecord);
            });
        }
        PmsPatient pmsPatient = BeanUtil.copyProperties(pmsArchivesInsert, PmsPatient.class);
        PmsPatientExample pmsPatientExample = new PmsPatientExample();
        pmsPatientExample.createCriteria().andIdCardEqualTo(pmsArchivesInsert.getIdCard()).andDelFlagEqualTo((byte) 0);
        List<PmsPatient> patientList = pmsPatientMapper.selectByExample(pmsPatientExample);
        if (CollectionUtil.isNotEmpty(patientList)) {
            pmsPatient.setId(patientList.get(0).getId());
            pmsPatientMapper.updateByPrimaryKeySelective(pmsPatient);
        }else {
            pmsPatient.setId(IdUtil.getSnowflake().nextId());
            pmsPatientMapper.insertSelective(pmsPatient);
        }
        if (pmsArchivesInsert.getType() == 2) {
            BlsZlInfoExample blsZlInfoExample = new BlsZlInfoExample();
            blsZlInfoExample.createCriteria().andDelFlagEqualTo((byte) 0).andPatientIdEqualTo(pmsPatient.getId());
            List<BlsZlInfo> blsZlInfos = blsZlInfoMapper.selectByExample(blsZlInfoExample);
            if (CollectionUtil.isEmpty(blsZlInfos)) {
                BlsZlInfo blsZlInfo = new BlsZlInfo();
                blsZlInfo.setId(IdUtil.getSnowflake().nextId());
                blsZlInfo.setPatientId(pmsPatient.getId());
                blsZlInfo.setCreateBy(SecurityUtils.getUsername());
                blsZlInfo.setCreateTime(new Date());
                blsZlInfo.setSource(0);
                blsZlInfoMapper.insertSelective(blsZlInfo);
                if (CollectionUtil.isNotEmpty(recordDtoList)) {
                    for (PmsArchivesDto.ArchivesRecordDto archivesRecordDto : recordDtoList) {
                        BlsZlInfoRecord blsZlInfoRecord = BeanUtil.copyProperties(archivesRecordDto, BlsZlInfoRecord.class);
                        blsZlInfoRecord.setId(IdUtil.getSnowflake().nextId());
                        blsZlInfoRecord.setBlId(blsZlInfo.getId());
                        blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord);
                    }
                }
                try {
                    BlsZlInfoRecord blsZlInfoRecord1 = new BlsZlInfoRecord();
                    blsZlInfoRecord1.setDelFlag((byte) 0);
                    blsZlInfoRecord1.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord1.setCode("SF-PF-NIHSS-NUM");
                    blsZlInfoRecord1.setBlId(blsZlInfo.getId());
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord1);

                    BlsZlInfoRecord blsZlInfoRecord2 = new BlsZlInfoRecord();
                    blsZlInfoRecord2.setDelFlag((byte) 0);
                    blsZlInfoRecord2.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord2.setCode("FILES-UNIT");
                    blsZlInfoRecord2.setBlId(blsZlInfo.getId());
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord2);

                    BlsZlInfoRecord blsZlInfoRecord3 = new BlsZlInfoRecord();
                    blsZlInfoRecord3.setDelFlag((byte) 0);
                    blsZlInfoRecord3.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord3.setCode("JMRS-120-NIHSS");
                    blsZlInfoRecord3.setBlId(blsZlInfo.getId());
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord3);

                    BlsZlInfoRecord blsZlInfoRecord4 = new BlsZlInfoRecord();
                    blsZlInfoRecord4.setDelFlag((byte) 0);
                    blsZlInfoRecord4.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord4.setCode("XGZL-ZHZT-TIME");
                    blsZlInfoRecord4.setBlId(blsZlInfo.getId());
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord4);

                    BlsZlInfoRecord blsZlInfoRecord5 = new BlsZlInfoRecord();
                    blsZlInfoRecord5.setDelFlag((byte) 0);
                    blsZlInfoRecord5.setBlId(blsZlInfo.getId());
                    blsZlInfoRecord5.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord5.setCode("SF-WXYS-PBG");
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord5);

                    BlsZlInfoRecord blsZlInfoRecord6 = new BlsZlInfoRecord();
                    blsZlInfoRecord6.setDelFlag((byte) 0);
                    blsZlInfoRecord6.setBlId(blsZlInfo.getId());
                    blsZlInfoRecord6.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord6.setCode("ZL-CYPG-NIHSS-NUM");
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord6);

                    BlsZlInfoRecord blsZlInfoRecord7 = new BlsZlInfoRecord();
                    blsZlInfoRecord7.setDelFlag((byte) 0);
                    blsZlInfoRecord7.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord7.setCode("ZL-RYPG-MRS");
                    blsZlInfoRecord7.setBlId(blsZlInfo.getId());
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord7);

                    BlsZlInfoRecord blsZlInfoRecord8 = new BlsZlInfoRecord();
                    blsZlInfoRecord8.setDelFlag((byte) 0);
                    blsZlInfoRecord8.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord8.setBlId(blsZlInfo.getId());
                    blsZlInfoRecord8.setCode("ZL-RYPG-DBP");
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord8);

                    BlsZlInfoRecord blsZlInfoRecord9 = new BlsZlInfoRecord();
                    blsZlInfoRecord9.setDelFlag((byte) 0);
                    blsZlInfoRecord9.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord9.setCode("ZL-JMRS-NIHSS-9-NUM");
                    blsZlInfoRecord9.setBlId(blsZlInfo.getId());
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord9);

                    BlsZlInfoRecord blsZlInfoRecord10 = new BlsZlInfoRecord();
                    blsZlInfoRecord10.setDelFlag((byte) 0);
                    blsZlInfoRecord10.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord10.setCode("ZL-JRZT-NIHSS-9-NUM");
                    blsZlInfoRecord10.setBlId(blsZlInfo.getId());
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord10);

                    BlsZlInfoRecord blsZlInfoRecord11 = new BlsZlInfoRecord();
                    blsZlInfoRecord11.setDelFlag((byte) 0);
                    blsZlInfoRecord11.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord11.setCode("ZL-YXJC-QT");
                    blsZlInfoRecord11.setBlId(blsZlInfo.getId());
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord11);

                    BlsZlInfoRecord blsZlInfoRecord12 = new BlsZlInfoRecord();
                    blsZlInfoRecord12.setDelFlag((byte) 0);
                    blsZlInfoRecord12.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord12.setBlId(blsZlInfo.getId());
                    blsZlInfoRecord12.setCode("ZL-NCX-JLDYSJ");
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord12);

                    BlsZlInfoRecord blsZlInfoRecord13 = new BlsZlInfoRecord();
                    blsZlInfoRecord13.setDelFlag((byte) 0);
                    blsZlInfoRecord13.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord13.setCode("ZL-DML-SS-BJSDMLQK");
                    blsZlInfoRecord13.setBlId(blsZlInfo.getId());
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord13);

                    BlsZlInfoRecord blsZlInfoRecord14 = new BlsZlInfoRecord();
                    blsZlInfoRecord14.setDelFlag((byte) 0);
                    blsZlInfoRecord14.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord14.setCode("ZL-CEACAS-BP");
                    blsZlInfoRecord14.setBlId(blsZlInfo.getId());
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord14);

                    BlsZlInfoRecord blsZlInfoRecord15 = new BlsZlInfoRecord();
                    blsZlInfoRecord15.setDelFlag((byte) 0);
                    blsZlInfoRecord15.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord15.setCode("ZL-JRZT-YWZL-I48-PLT");
                    blsZlInfoRecord15.setBlId(blsZlInfo.getId());
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord15);

                    BlsZlInfoRecord blsZlInfoRecord16 = new BlsZlInfoRecord();
                    blsZlInfoRecord16.setDelFlag((byte) 0);
                    blsZlInfoRecord16.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord16.setBlId(blsZlInfo.getId());
                    blsZlInfoRecord16.setCode("ZL-TOAST-YWB");
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord16);

                    BlsZlInfoRecord blsZlInfoRecord17 = new BlsZlInfoRecord();
                    blsZlInfoRecord17.setDelFlag((byte) 0);
                    blsZlInfoRecord17.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord17.setCode("ZL-ZYZL-KFDL-TYPE");
                    blsZlInfoRecord17.setBlId(blsZlInfo.getId());
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord17);

                    BlsZlInfoRecord blsZlInfoRecord18 = new BlsZlInfoRecord();
                    blsZlInfoRecord18.setDelFlag((byte) 0);
                    blsZlInfoRecord18.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord18.setCode("ZL-YWBDJ-SS-BFZ-QT");
                    blsZlInfoRecord18.setBlId(blsZlInfo.getId());
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord18);

                    BlsZlInfoRecord blsZlInfoRecord19 = new BlsZlInfoRecord();
                    blsZlInfoRecord19.setDelFlag((byte) 0);
                    blsZlInfoRecord19.setId(IdUtil.getSnowflake().nextId());
                    blsZlInfoRecord19.setCode("ZL-AVM-C-QT");
                    blsZlInfoRecord19.setBlId(blsZlInfo.getId());
                    blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord19);

                }catch (Exception e) {
                    log.info("添加子项出错");
                }

            }
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updatePmsArchives(PmsArchivesDto.Update pmsArchivesUpdate) {
        // 检查身份证号是否重复
        if (StrUtil.isNotBlank(pmsArchivesUpdate.getIdCard())) {
            PmsPatientExample pmsPatientExample = new PmsPatientExample();
            pmsPatientExample.createCriteria().andDelFlagEqualTo((byte) 0).andIdCardEqualTo(pmsArchivesUpdate.getIdCard()).andIdNotEqualTo(pmsArchivesUpdate.getFileNum());
            List<PmsPatient> pmsArchivesList = pmsPatientMapper.selectByExample(pmsPatientExample);
            if (!pmsArchivesList.isEmpty()) {
                throw new BaseException(ErrorConstants.DUPLICATE_ID_CARD);
            }
        }
        // 修改实体
        PmsArchives pmsArchives = BeanUtil.copyProperties(pmsArchivesUpdate, PmsArchives.class);
        pmsArchives.setUpdateBy(SecurityUtils.getUsername());
        pmsArchives.setUpdateTime(new Date());
        int i = pmsArchivesMapper.updateByPrimaryKeySelective(pmsArchives);
//        if (i > 0 && !pmsArchivesUpdate.getRecordDtoList().isEmpty()) {
//            pmsArchivesUpdate.getRecordDtoList().forEach(recordDto -> {
//                PmsArchivesRecord pmsArchivesRecord = BeanUtil.copyProperties(pmsArchivesUpdate.getRecordDtoList(), PmsArchivesRecord.class);
//                pmsArchivesRecord.setAnswer(pmsArchivesRecord.getAnswer());
//                pmsArchivesRecord.setUpdatedAt(new Date());
//                PmsArchivesRecordExample pmsArchivesRecordExample = new PmsArchivesRecordExample();
//                pmsArchivesRecordExample.createCriteria().andCodeEqualTo(recordDto.getCode()).andArchivesIdEqualTo(pmsArchivesUpdate.getId());
//                pmsArchivesRecordMapper.updateByExampleSelective(pmsArchivesRecord, pmsArchivesRecordExample);
//            });
//        }
        //修改患者档案
        PmsPatientExample pmsPatientExample = new PmsPatientExample();
        pmsPatientExample.createCriteria().andIdCardEqualTo(pmsArchivesUpdate.getIdCard()).andDelFlagEqualTo((byte) 0);
        List<PmsPatient> patientList = pmsPatientMapper.selectByExample(pmsPatientExample);
        if (CollectionUtil.isNotEmpty(patientList)) {
            PmsPatient pmsPatient = BeanUtil.copyProperties(pmsArchivesUpdate, PmsPatient.class);
            pmsPatient.setId(patientList.get(0).getId());
            pmsPatientMapper.updateByPrimaryKeySelective(pmsPatient);
        }
        List<PmsArchivesDto.ArchivesRecordDto> recordDtoList = pmsArchivesUpdate.getRecordDtoList();
        BlsZlInfoRecordExample blsZlInfoExample = new BlsZlInfoRecordExample();
        blsZlInfoExample.createCriteria().andDelFlagEqualTo((byte) 0).andBlIdEqualTo(pmsArchivesUpdate.getId());
        BlsZlInfoRecord blsZlInfoRecord = new BlsZlInfoRecord();
        blsZlInfoRecord.setDelFlag((byte) 1);
        blsZlInfoRecordMapper.updateByExampleSelective(blsZlInfoRecord, blsZlInfoExample);
        if (CollectionUtil.isNotEmpty(recordDtoList)) {
            for (PmsArchivesDto.ArchivesRecordDto archivesRecordDto : recordDtoList) {
                BlsZlInfoRecord blsZlInfoRecord1 = BeanUtil.copyProperties(archivesRecordDto, BlsZlInfoRecord.class);
                blsZlInfoRecord1.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord1.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord1);

                if (StrUtil.isNotEmpty(archivesRecordDto.getAnswer()) && "ZL-JBXX-ZYTIME".equals(archivesRecordDto.getCode())) {
                    BlsZlInfo blsZlInfo = blsZlInfoMapper.selectByPrimaryKey(pmsArchivesUpdate.getId());
                    if (blsZlInfo != null) {
                        blsZlInfo.setRysj(DateUtil.parse(archivesRecordDto.getAnswer()));
                    }
                    blsZlInfoMapper.updateByPrimaryKeySelective(blsZlInfo);
                }
            }
            try {
                BlsZlInfoRecord blsZlInfoRecord1 = new BlsZlInfoRecord();
                blsZlInfoRecord1.setDelFlag((byte) 0);
                blsZlInfoRecord1.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord1.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecord1.setCode("SF-PF-NIHSS-NUM");
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord1);

                BlsZlInfoRecord blsZlInfoRecord2 = new BlsZlInfoRecord();
                blsZlInfoRecord2.setDelFlag((byte) 0);
                blsZlInfoRecord2.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord2.setCode("FILES-UNIT");
                blsZlInfoRecord2.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord2);

                BlsZlInfoRecord blsZlInfoRecord3 = new BlsZlInfoRecord();
                blsZlInfoRecord3.setDelFlag((byte) 0);
                blsZlInfoRecord3.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord3.setCode("JMRS-120-NIHSS");
                blsZlInfoRecord3.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord3);

                BlsZlInfoRecord blsZlInfoRecord4 = new BlsZlInfoRecord();
                blsZlInfoRecord4.setDelFlag((byte) 0);
                blsZlInfoRecord4.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord4.setCode("XGZL-ZHZT-TIME");
                blsZlInfoRecord4.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord4);

                BlsZlInfoRecord blsZlInfoRecord5 = new BlsZlInfoRecord();
                blsZlInfoRecord5.setDelFlag((byte) 0);
                blsZlInfoRecord5.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecord5.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord5.setCode("SF-WXYS-PBG");
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord5);

                BlsZlInfoRecord blsZlInfoRecord6 = new BlsZlInfoRecord();
                blsZlInfoRecord6.setDelFlag((byte) 0);
                blsZlInfoRecord6.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecord6.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord6.setCode("ZL-CYPG-NIHSS-NUM");
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord6);

                BlsZlInfoRecord blsZlInfoRecord7 = new BlsZlInfoRecord();
                blsZlInfoRecord7.setDelFlag((byte) 0);
                blsZlInfoRecord7.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord7.setCode("ZL-RYPG-MRS");
                blsZlInfoRecord7.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord7);

                BlsZlInfoRecord blsZlInfoRecord8 = new BlsZlInfoRecord();
                blsZlInfoRecord8.setDelFlag((byte) 0);
                blsZlInfoRecord8.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord8.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecord8.setCode("ZL-RYPG-DBP");
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord8);

                BlsZlInfoRecord blsZlInfoRecord9 = new BlsZlInfoRecord();
                blsZlInfoRecord9.setDelFlag((byte) 0);
                blsZlInfoRecord9.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord9.setCode("ZL-JMRS-NIHSS-9-NUM");
                blsZlInfoRecord9.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord9);

                BlsZlInfoRecord blsZlInfoRecord10 = new BlsZlInfoRecord();
                blsZlInfoRecord10.setDelFlag((byte) 0);
                blsZlInfoRecord10.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord10.setCode("ZL-JRZT-NIHSS-9-NUM");
                blsZlInfoRecord10.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord10);

                BlsZlInfoRecord blsZlInfoRecord11 = new BlsZlInfoRecord();
                blsZlInfoRecord11.setDelFlag((byte) 0);
                blsZlInfoRecord11.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord11.setCode("ZL-YXJC-QT");
                blsZlInfoRecord11.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord11);

                BlsZlInfoRecord blsZlInfoRecord12 = new BlsZlInfoRecord();
                blsZlInfoRecord12.setDelFlag((byte) 0);
                blsZlInfoRecord12.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord12.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecord12.setCode("ZL-NCX-JLDYSJ");
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord12);

                BlsZlInfoRecord blsZlInfoRecord13 = new BlsZlInfoRecord();
                blsZlInfoRecord13.setDelFlag((byte) 0);
                blsZlInfoRecord13.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord13.setCode("ZL-DML-SS-BJSDMLQK");
                blsZlInfoRecord13.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord13);

                BlsZlInfoRecord blsZlInfoRecord14 = new BlsZlInfoRecord();
                blsZlInfoRecord14.setDelFlag((byte) 0);
                blsZlInfoRecord14.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord14.setCode("ZL-CEACAS-BP");
                blsZlInfoRecord14.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord14);

                BlsZlInfoRecord blsZlInfoRecord15 = new BlsZlInfoRecord();
                blsZlInfoRecord15.setDelFlag((byte) 0);
                blsZlInfoRecord15.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord15.setCode("ZL-JRZT-YWZL-I48-PLT");
                blsZlInfoRecord15.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord15);

                BlsZlInfoRecord blsZlInfoRecord16 = new BlsZlInfoRecord();
                blsZlInfoRecord16.setDelFlag((byte) 0);
                blsZlInfoRecord16.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord16.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecord16.setCode("ZL-TOAST-YWB");
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord16);

                BlsZlInfoRecord blsZlInfoRecord17 = new BlsZlInfoRecord();
                blsZlInfoRecord17.setDelFlag((byte) 0);
                blsZlInfoRecord17.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord17.setCode("ZL-ZYZL-KFDL-TYPE");
                blsZlInfoRecord17.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord17);

                BlsZlInfoRecord blsZlInfoRecord18 = new BlsZlInfoRecord();
                blsZlInfoRecord18.setDelFlag((byte) 0);
                blsZlInfoRecord18.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord18.setCode("ZL-YWBDJ-SS-BFZ-QT");
                blsZlInfoRecord18.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord18);

                BlsZlInfoRecord blsZlInfoRecord19 = new BlsZlInfoRecord();
                blsZlInfoRecord19.setDelFlag((byte) 0);
                blsZlInfoRecord19.setId(IdUtil.getSnowflake().nextId());
                blsZlInfoRecord19.setCode("ZL-AVM-C-QT");
                blsZlInfoRecord19.setBlId(pmsArchivesUpdate.getId());
                blsZlInfoRecordMapper.insertSelective(blsZlInfoRecord19);

            }catch (Exception e) {
                log.info("添加子项出错");
            }
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deletePmsArchives(PmsArchivesDto.Delete PmsArchivesDelete) {
        PmsArchivesExample pmsArchivesExample = new PmsArchivesExample();
        pmsArchivesExample.createCriteria().andIdIn(PmsArchivesDelete.getIdList());
        PmsArchives pmsArchives = new PmsArchives();
        pmsArchives.setDelFlag((byte) 2);
        int i = pmsArchivesMapper.updateByExampleSelective(pmsArchives, pmsArchivesExample);
        if (i > 0) {
            PmsArchivesRecordExample pmsArchivesRecordExample = new PmsArchivesRecordExample();
            pmsArchivesRecordExample.createCriteria().andArchivesIdIn(PmsArchivesDelete.getIdList());
            PmsArchivesRecord pmsArchivesRecord = new PmsArchivesRecord();
            pmsArchivesRecord.setDelFlag((byte) 2);
            pmsArchivesRecordMapper.updateByExampleSelective(pmsArchivesRecord, pmsArchivesRecordExample);
        }
        return i;
    }

    @Override
    public PageInfo<PmsArchivesVo.Result> listPmsArchives(BaseDto<PmsArchivesDto.Query> list) {
        PmsArchivesDto.Query param = list.getParam();
        if (param.getTagId() != null) {
            //根据档案菜单查询变量
            CmsArchivesCodeTagExample cmsArchivesCodeTagExample = new CmsArchivesCodeTagExample();
            cmsArchivesCodeTagExample.createCriteria()
                    .andArchivesTagIdEqualTo(param.getTagId()).andDelFlagEqualTo((byte) 0);
            List<CmsArchivesCodeTag> cmsArchivesCodeTagList = cmsArchivesCodeTagMapper.selectByExample(cmsArchivesCodeTagExample);
            if (CollectionUtil.isNotEmpty(cmsArchivesCodeTagList)) {
                List<String> collect = cmsArchivesCodeTagList.stream().map(CmsArchivesCodeTag::getCode).collect(Collectors.toList());
                param.setCodeList(collect);
            }
        }
        if (CollectionUtil.isNotEmpty(param.getOperationWay())) {
            if (param.getOperationWay().contains("0")) {
                param.setIsJmrs("1");
            }
        }

        if (list.getPageNum() > 0) {
            PageHelper.startPage(list.getPageNum(), list.getPageSize(), list.getSort());
        }
        List<PmsArchivesVo.Result> mainList = pmsArchivesDao.selectIds(list.getParam());
        PageInfo<PmsArchivesVo.Result> pageInfo = new PageInfo<>(mainList);
        if (CollectionUtil.isNotEmpty(mainList)) {
            List<Long> ids = pageInfo.getList().stream().map(PmsArchivesVo.Result::getId).collect(Collectors.toList());
            List<PmsArchivesVo.Result> resultList = pmsArchivesDao.selectInfo(ids, new ArrayList<>(), null);
            //查询code
            List<TCode> codeList1 = codeServier.queryByCodeList(param.getCodeList());
            Map<String, TCode> map = new HashMap<>();
            if (CollectionUtil.isNotEmpty(codeList1)) {
                map = codeList1.stream().collect(Collectors.toMap(TCode::getCode, Function.identity()));
            }
            for (PmsArchivesVo.Result result : resultList) {
                List<PmsArchivesVo.PmsArchivesRecordVo> recordVoList = result.getRecordVoList();
                if (CollectionUtil.isEmpty(recordVoList)) {
                    continue;
                }
                List<PmsArchivesVo.PmsArchivesRecordVo> newList = new ArrayList<>();
                List<String> codeList = recordVoList.stream().map(PmsArchivesVo.PmsArchivesRecordVo::getCode).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(param.getCodeList())) {
                    for (String code : param.getCodeList()) {
                        if (codeList.contains(code)) {
                            continue;
                        }
                        TCode tCode = map.get(code);
                        PmsArchivesVo.PmsArchivesRecordVo pmsArchivesRecordVo = new PmsArchivesVo.PmsArchivesRecordVo();
                        pmsArchivesRecordVo.setCode(code);
                        if (tCode != null) {
                            pmsArchivesRecordVo.setOptions(tCode.getOptions());
                            pmsArchivesRecordVo.setShowType(tCode.getShowType());
                            pmsArchivesRecordVo.setOperatorName(tCode.getOperatorName());
                            pmsArchivesRecordVo.setCodeName(tCode.getText());
                        }
                        newList.add(pmsArchivesRecordVo);
                    }
                }
                recordVoList.addAll(newList);
                List<PmsArchivesVo.PmsArchivesRecordVo> collect = recordVoList.stream().sorted(Comparator.comparing(PmsArchivesVo.PmsArchivesRecordVo::getSort)).collect(Collectors.toList());
                result.setRecordVoList(collect);

                try {
                    getIndex(result);
                } catch (Exception e) {
                    log.error("质控指标生成错误:{}", result.getId());
                }
            }
            pageInfo.setList(resultList);
//            pageInfo.setTotal(resultList.size());
        }
        return pageInfo;
    }

    @Override
    public PageInfo<PmsArchivesVo.Result> listPmsArchive(BaseDto<PmsArchivesDto.Query> list) {
        PmsArchivesDto.Query param = list.getParam();
        if (param.getTagId() != null) {
            //根据档案菜单查询变量
            CmsArchivesCodeTagExample cmsArchivesCodeTagExample = new CmsArchivesCodeTagExample();
            cmsArchivesCodeTagExample.createCriteria()
                    .andArchivesTagIdEqualTo(param.getTagId()).andDelFlagEqualTo((byte) 0);
            List<CmsArchivesCodeTag> cmsArchivesCodeTagList = cmsArchivesCodeTagMapper.selectByExample(cmsArchivesCodeTagExample);
            if (CollectionUtil.isNotEmpty(cmsArchivesCodeTagList)) {
                List<String> collect = cmsArchivesCodeTagList.stream().map(CmsArchivesCodeTag::getCode).collect(Collectors.toList());
                param.setCodeList(collect);
            }
        }
        PageHelper.startPage(list.getPageNum(), list.getPageSize(), list.getSort());
        List<PmsArchivesVo.Result> mainList = pmsArchivesDao.selectIds(list.getParam());
        PageInfo<PmsArchivesVo.Result> resultPageInfo = new PageInfo<>(mainList);
        if (CollectionUtil.isNotEmpty(mainList)) {
            List<Long> ids = mainList.stream().map(PmsArchivesVo.Result::getId).collect(Collectors.toList());
            List<PmsArchivesVo.Result> resultList = pmsArchivesDao.selectInfo1(ids, new ArrayList<>(), null);
            //查询code
            List<TCode> codeList1 = codeServier.queryByCodeList(param.getCodeList());
            Map<String, TCode> map = new HashMap<>();
            if (CollectionUtil.isNotEmpty(codeList1)) {
                map = codeList1.stream().collect(Collectors.toMap(TCode::getCode, Function.identity()));
            }
            for (PmsArchivesVo.Result result : resultList) {
                List<PmsArchivesVo.PmsArchivesRecordVo> recordVoList = result.getRecordVoList();
                List<PmsArchivesVo.PmsArchivesRecordVo> newList = new ArrayList<>();
                List<String> codeList = recordVoList.stream().map(PmsArchivesVo.PmsArchivesRecordVo::getCode).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(param.getCodeList())) {
                    for (String code : param.getCodeList()) {
                        if (codeList.contains(code)) {
                            continue;
                        }
                        TCode tCode = map.get(code);
                        PmsArchivesVo.PmsArchivesRecordVo pmsArchivesRecordVo = new PmsArchivesVo.PmsArchivesRecordVo();
                        pmsArchivesRecordVo.setCode(code);
                        if (tCode != null) {
                            pmsArchivesRecordVo.setOptions(tCode.getOptions());
                            pmsArchivesRecordVo.setShowType(tCode.getShowType());
                            pmsArchivesRecordVo.setOperatorName(tCode.getOperatorName());
                            pmsArchivesRecordVo.setCodeName(tCode.getText());
                        }
                        newList.add(pmsArchivesRecordVo);
                    }
                }
                recordVoList.addAll(newList);
                result.setRecordVoList(recordVoList);
                try {
                    getIndex(result);
                } catch (Exception e) {
                    log.error("质控指标生成错误:{}", result.getId());
                }
            }
            resultPageInfo.setList(resultList);
        }
        return resultPageInfo;
    }

    /**
     * 生成质控指标code
     *
     * @return
     */
    private PmsArchivesVo.Result getIndex(PmsArchivesVo.Result result) throws ParseException {
        List<PmsArchivesVo.PmsArchivesRecordVo> recordVoList = result.getRecordVoList();
        if (CollectionUtil.isNotEmpty(recordVoList)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Map<String, PmsArchivesVo.PmsArchivesRecordVo> map = recordVoList.stream().collect(Collectors.toMap(PmsArchivesVo.PmsArchivesRecordVo::getCode, Function.identity()));
            //发病时间
            PmsArchivesVo.PmsArchivesRecordVo fbsjRecordVo = map.get(JBXX_FBSJ.getKey());
            Date fbsj = new Date();
            if (fbsjRecordVo != null && StrUtil.isNotEmpty(fbsjRecordVo.getAnswer())) {
                fbsj = sdf.parse(fbsjRecordVo.getAnswer());
            }
            //到院时间
            PmsArchivesVo.PmsArchivesRecordVo dysjRecordVo = map.get(JBXX_DY_TIME.getKey());
            Date dysj = new Date();
            if (dysjRecordVo != null && StrUtil.isNotEmpty(dysjRecordVo.getAnswer())) {
                dysj = sdf.parse(dysjRecordVo.getAnswer());
            }
            //ct时间
            PmsArchivesVo.PmsArchivesRecordVo ctsjRecordVo = map.get(RYPG_CT_JCKS_TIME.getKey());
            Date ctsj = new Date();
            if (ctsjRecordVo != null) {
                if (StrUtil.isNotEmpty(ctsjRecordVo.getAnswer())) {
                    ctsj = sdf.parse(ctsjRecordVo.getAnswer());
                }
            }
            //溶栓时间
            PmsArchivesVo.PmsArchivesRecordVo rssjRecordVo = map.get(JMRS_TIME.getKey());
            Date rssj = new Date();
            if (rssjRecordVo != null && StrUtil.isNotEmpty(rssjRecordVo.getAnswer())) {
                rssj = sdf.parse(rssjRecordVo.getAnswer());
            }
            //穿刺完成时间
            PmsArchivesVo.PmsArchivesRecordVo ccwcsjRecordVo = map.get(ZL_JRZT_CCWCSJ.getKey());
            Date zqsj = new Date();
            if (ccwcsjRecordVo != null && StrUtil.isNotEmpty(ccwcsjRecordVo.getAnswer())) {
                zqsj = sdf.parse(ccwcsjRecordVo.getAnswer());
            }
            //血管再通时间
            PmsArchivesVo.PmsArchivesRecordVo xgztsjRecordVo = map.get(XGZL_SCZT_TIME.getKey());
            Date xgztsj = new Date();
            if (xgztsjRecordVo != null && StrUtil.isNotEmpty(xgztsjRecordVo.getAnswer())) {
                xgztsj = sdf.parse(xgztsjRecordVo.getAnswer());
            }
            //计算质控指标值
            //DNT 到达医院(到达急诊时间)-溶栓开始的时间(团注开始时间)
            if (dysj != null && rssj != null) {
                Integer dnt = (int) ((rssj.getTime() - dysj.getTime()) / 1000);
                result.setDnt(dnt);
            }
            //ODT 发病(发病时间/最后正常时间) 达到医院的时间(到达医院时间，通常是到达急诊时间)
            if (fbsj != null && dysj != null) {
                Integer odt = (int) ((dysj.getTime() - fbsj.getTime()) / 1000);
                result.setOdt(odt);
            }
            //DIT 到达医院(到达急诊时间)影像时间(CT/核磁完成时间)
            if (dysj != null && ctsj != null) {
                Integer dit = (int) ((ctsj.getTime() - dysj.getTime()) / 1000);
                result.setDit(dit);
            }
            //ONT 发病-溶栓开始的时间
            if (fbsj != null && rssj != null) {
                Integer ont = (int) ((rssj.getTime() - fbsj.getTime()) / 1000);
                result.setOnt(ont);
            }
            //DPT 到达医院-置鞘时间(取栓)
            if (dysj != null && zqsj != null) {
                Integer dpt = (int) ((zqsj.getTime() - dysj.getTime()) / 1000);
                result.setDpt(dpt);
            }
            //OPT 发病-置鞘时间(取栓)
            if (fbsj != null && zqsj != null) {
                Integer opt = (int) ((zqsj.getTime() - fbsj.getTime()) / 1000);
                result.setOpt(opt);
            }
            //PRT 置鞘-血管再通时间
            if (zqsj != null && xgztsj != null) {
                Integer prt = (int) ((xgztsj.getTime() - zqsj.getTime()) / 1000);
                result.setPrt(prt);
            }
        }
        return result;
    }


//    @Override
//    public PmsArchivesVo.Result queryDetail(PmsArchivesDto.Query dto) {
//        if (dto.getTagId() != null) {
//            //根据档案菜单查询变量
//            CmsArchivesCodeTagExample cmsArchivesCodeTagExample = new CmsArchivesCodeTagExample();
//            cmsArchivesCodeTagExample.createCriteria()
//                    .andArchivesTagIdEqualTo(dto.getTagId()).andDelFlagEqualTo((byte) 0);
//            cmsArchivesCodeTagExample.setOrderByClause("sort ");
//            List<CmsArchivesCodeTag> cmsArchivesCodeTagList = cmsArchivesCodeTagMapper.selectByExample(cmsArchivesCodeTagExample);
//            if (CollectionUtil.isNotEmpty(cmsArchivesCodeTagList)) {
//                List<String> collect = cmsArchivesCodeTagList.stream().map(CmsArchivesCodeTag::getCode).collect(Collectors.toList());
//                dto.setCodeList(collect);
//            }
//        }
//
//        //查询主数据
//        List<PmsArchivesVo.Result> resultList = pmsArchivesDao.selectInfo(CollectionUtil.newArrayList(dto.getId()), CollectionUtil.newArrayList(), dto.getTagId());
//
//        //查询code
//        List<TCode> codeList1 = codeServier.queryByCodeList(dto.getCodeList());
//        Map<String, TCode> map = new HashMap<>();
//        if (CollectionUtil.isNotEmpty(codeList1)) {
//            map = codeList1.stream().collect(Collectors.toMap(TCode::getCode, Function.identity()));
//        }
//        for (PmsArchivesVo.Result result : resultList) {
//            result.setRecordVoList(new ArrayList<>());
//            //查询子数据
//            PmsArchivesRecordExample pmsArchivesRecordExample = new PmsArchivesRecordExample();
//            PmsArchivesRecordExample.Criteria criteria = pmsArchivesRecordExample.createCriteria().andDelFlagEqualTo((byte) 0).andArchivesIdEqualTo(result.getId());
//            if (CollectionUtil.isNotEmpty(dto.getCodeList())) {
//                criteria.andCodeIn(dto.getCodeList());
//            }
//
//            List<PmsArchivesRecord> pmsArchivesRecords = pmsArchivesRecordMapper.selectByExample(pmsArchivesRecordExample);
//            List<PmsArchivesVo.PmsArchivesRecordVo> recordVoList = result.getRecordVoList();
//            if (CollectionUtil.isNotEmpty(pmsArchivesRecords)) {
//                recordVoList = BeanUtil.copyToList(pmsArchivesRecords, PmsArchivesVo.PmsArchivesRecordVo.class);
//                for (PmsArchivesVo.PmsArchivesRecordVo pmsArchivesRecordVo : recordVoList) {
//                    TCode tCode = map.get(pmsArchivesRecordVo.getCode());
//                    if (tCode != null) {
//                        pmsArchivesRecordVo.setCodeName(tCode.getText());
//                        pmsArchivesRecordVo.setOptions(tCode.getOptions());
//                        pmsArchivesRecordVo.setShowType(tCode.getShowType());
//                        pmsArchivesRecordVo.setOperatorName(tCode.getOperatorName());
//                    }
//                }
//            }
//
//            List<PmsArchivesVo.PmsArchivesRecordVo> newList = new ArrayList<>();
////                if (CollectionUtil.isNotEmpty(recordVoList) && param.getTagId() != null) {
//            List<String> codeList = recordVoList.stream().map(PmsArchivesVo.PmsArchivesRecordVo::getCode).collect(Collectors.toList());
//            for (String code : dto.getCodeList()) {
//                if (codeList.contains(code)) {
//                    continue;
//                }
//                TCode tCode = map.get(code);
//                PmsArchivesVo.PmsArchivesRecordVo pmsArchivesRecordVo = new PmsArchivesVo.PmsArchivesRecordVo();
//                pmsArchivesRecordVo.setCode(code);
//                if (tCode != null) {
//                    pmsArchivesRecordVo.setOptions(tCode.getOptions());
//                    pmsArchivesRecordVo.setShowType(tCode.getShowType());
//                    pmsArchivesRecordVo.setOperatorName(tCode.getOperatorName());
//                    pmsArchivesRecordVo.setCodeName(tCode.getText());
////                    pmsArchivesRecordVo.setSort();
//
//                }
//                newList.add(pmsArchivesRecordVo);
//            }
//            recordVoList.addAll(newList);
//
////            resultList.stream().sorted(Comparator.comparing());
//
//            result.setRecordVoList(recordVoList);
//            return result;
//        }
//        return null;
//    }

    @Override
    public PmsArchivesVo.Result  queryDetail(PmsArchivesDto.Query dto) {
        if (dto.getTagId() != null) {
            //根据档案菜单查询变量
            CmsArchivesCodeTagExample cmsArchivesCodeTagExample = new CmsArchivesCodeTagExample();
            cmsArchivesCodeTagExample.createCriteria()
                    .andArchivesTagIdEqualTo(dto.getTagId()).andDelFlagEqualTo((byte) 0);
            cmsArchivesCodeTagExample.setOrderByClause("sort ");
            List<CmsArchivesCodeTag> cmsArchivesCodeTagList = cmsArchivesCodeTagMapper.selectByExample(cmsArchivesCodeTagExample);
            if (CollectionUtil.isNotEmpty(cmsArchivesCodeTagList)) {
                List<String> collect = cmsArchivesCodeTagList.stream().map(CmsArchivesCodeTag::getCode).collect(Collectors.toList());
                dto.setCodeList(collect);
            }
        }

        //查询主数据
        List<PmsArchivesVo.Result> resultList = pmsArchivesDao.selectInfo(CollectionUtil.newArrayList(dto.getId()), CollectionUtil.newArrayList(), dto.getTagId());

        //查询code
        List<TCode> codeList1 = codeServier.queryByCodeList(dto.getCodeList());
        Map<String, TCode> map = new HashMap<>();
        if (CollectionUtil.isNotEmpty(codeList1)) {
            map = codeList1.stream().collect(Collectors.toMap(TCode::getCode, Function.identity()));
        }
        for (PmsArchivesVo.Result result : resultList) {
            result.setRecordVoList(new ArrayList<>());
            //查询子数据
            BlsZlInfoRecordExample pmsArchivesRecordExample = new BlsZlInfoRecordExample();
            BlsZlInfoRecordExample.Criteria criteria = pmsArchivesRecordExample.createCriteria().andDelFlagEqualTo((byte) 0).andBlIdEqualTo(result.getId());
            if (CollectionUtil.isNotEmpty(dto.getCodeList())) {
                criteria.andCodeIn(dto.getCodeList());
            }
            List<BlsZlInfoRecord> pmsArchivesRecords = blsZlInfoRecordMapper.selectByExample(pmsArchivesRecordExample);
            List<PmsArchivesVo.PmsArchivesRecordVo> recordVoList = result.getRecordVoList();
            if (CollectionUtil.isNotEmpty(pmsArchivesRecords)) {
                recordVoList = BeanUtil.copyToList(pmsArchivesRecords, PmsArchivesVo.PmsArchivesRecordVo.class);
                for (PmsArchivesVo.PmsArchivesRecordVo pmsArchivesRecordVo : recordVoList) {
                    TCode tCode = map.get(pmsArchivesRecordVo.getCode());
                    if (tCode != null) {
                        pmsArchivesRecordVo.setCodeName(tCode.getText());
                        pmsArchivesRecordVo.setOptions(tCode.getOptions());
                        pmsArchivesRecordVo.setShowType(tCode.getShowType());
                        pmsArchivesRecordVo.setOperatorName(tCode.getOperatorName());
                    }
                }
            }

            List<PmsArchivesVo.PmsArchivesRecordVo> newList = new ArrayList<>();
//                if (CollectionUtil.isNotEmpty(recordVoList) && param.getTagId() != null) {
            List<String> codeList = recordVoList.stream().map(PmsArchivesVo.PmsArchivesRecordVo::getCode).collect(Collectors.toList());
            for (String code : dto.getCodeList()) {
                if (codeList.contains(code) || "PERSON-BIRTH".equals(code) || "ZL-JBXX-IDCARD".equals(code)) {
                    continue;
                }
                TCode tCode = map.get(code);
                PmsArchivesVo.PmsArchivesRecordVo pmsArchivesRecordVo = new PmsArchivesVo.PmsArchivesRecordVo();
                pmsArchivesRecordVo.setCode(code);
                if (tCode != null) {
                    pmsArchivesRecordVo.setOptions(tCode.getOptions());
                    pmsArchivesRecordVo.setShowType(tCode.getShowType());
                    pmsArchivesRecordVo.setOperatorName(tCode.getOperatorName());
                    pmsArchivesRecordVo.setCodeName(tCode.getText());
//                    pmsArchivesRecordVo.setSort();

                }
                newList.add(pmsArchivesRecordVo);
            }
            recordVoList.addAll(newList);

//            resultList.stream().sorted(Comparator.comparing());

            result.setRecordVoList(recordVoList);

            if (dto.getTagId() == 7) {
                String idCard = result.getIdCard();
                if (StrUtil.isNotEmpty(idCard)) {
                    PmsArchivesVo.PmsArchivesRecordVo pmsArchivesRecordVo = new PmsArchivesVo.PmsArchivesRecordVo();
                    pmsArchivesRecordVo.setCode("PERSON-BIRTH");
                    pmsArchivesRecordVo.setAnswer(IdcardUtil.getBirth(idCard));
                    pmsArchivesRecordVo.setOperatorName("date");
                    pmsArchivesRecordVo.setCodeName("出生日期");
                    pmsArchivesRecordVo.setCodeTagId(0L);
                    List<PmsArchivesVo.PmsArchivesRecordVo> recordVoList1 = result.getRecordVoList();
                    recordVoList1.add(pmsArchivesRecordVo);

                    PmsArchivesVo.PmsArchivesRecordVo pmsArchivesRecordVo2 = new PmsArchivesVo.PmsArchivesRecordVo();
                    pmsArchivesRecordVo2.setCode("ZL-JBXX-IDCARD");
                    pmsArchivesRecordVo2.setAnswer(idCard);
                    pmsArchivesRecordVo2.setCodeName("身份证号");
                    pmsArchivesRecordVo2.setOperatorName("input");
                    pmsArchivesRecordVo2.setCodeTagId(0L);
                    recordVoList1.add(pmsArchivesRecordVo2);
                    result.setRecordVoList(recordVoList1);
                }
            }

            return result;
        }
        return null;
    }

    @Override
    public void exportZyzl(PmsArchivesDto.Query dto) {
        BaseDto<PmsArchivesDto.Query> baseDto = new BaseDto<>();
        baseDto.setParam(dto);
        baseDto.setPageNum(-1);
        PageInfo<PmsArchivesVo.Result> pageInfoList = listPmsArchives(baseDto);
        List<PmsArchivesVo.Result> list = pageInfoList.getList();
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        BigExcelWriter writer = new BigExcelWriter();
        List<String> headList = new ArrayList<>();
        headList.add("患者姓名");
        headList.add("性别");
        headList.add("身份证号");
        headList.add("民族");
        headList.add("联系人姓名");
        headList.add("联系人电话");
        headList.add("住院号");
        headList.add("入院诊断");
        headList.add("住院医师");
        headList.add("所属科室");
        headList.add("治疗类型");
        headList.add("出院时间");
        headList.add("档案号");
        headList.add("档案状态");
        headList.add("住院总费用");
        headList.add("离院方式");
        headList.add("病案首页出院主要诊断编码");
        int row = 0;
//        for (int i = 0; i < blsZlInfoVos.size(); i++) {
//            row += 1;
//            excelWriter.writeCellValue(0, row, blsZlInfoVos.get(i).getName());
//            if (blsZlInfoVos.get(i).getSex() != null) {
//                excelWriter.writeCellValue(1, row, blsZlInfoVos.get(i).getSex() == 0 ? "女" : "男");//"性别",
//            }
//            excelWriter.writeCellValue(2, row, blsZlInfoVos.get(i).getNation());
//            excelWriter.writeCellValue(3, row, blsZlInfoVos.get(i).getIdCard());
//
//            if (StrUtil.isNotEmpty(blsZlInfoVos.get(i).getIdCard())) {
//                excelWriter.writeCellValue(4, row, IdcardUtil.getBirth(blsZlInfoVos.get(i).getIdCard()));
//                excelWriter.writeCellValue(5, row, IdcardUtil.getAgeByIdCard(blsZlInfoVos.get(i).getIdCard()));
//            }
//
//            List<BlsVo.BlsJzInfoVo.PmsArchivesRecordVo> recordVoList = blsZlInfoVos.get(i).getRecordVoList();
//            if (CollectionUtil.isEmpty(recordVoList)) {
//                continue;
//            }
//            Map<String, List<BlsVo.BlsJzInfoVo.PmsArchivesRecordVo>> map = recordVoList.stream().collect(Collectors.groupingBy(BlsVo.BlsJzInfoVo.PmsArchivesRecordVo::getCode));
//
//            excelWriter.writeCellValue(6, row, blsZlInfoVos.get(i).getName());
//            excelWriter.writeCellValue(7, row, blsZlInfoVos.get(i).getPhone());
//            excelWriter.writeCellValue(8, row, CollectionUtil.isNotEmpty(map.get(FILES_HEIGHT.getKey())) ? map.get(FILES_HEIGHT.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(9, row, CollectionUtil.isNotEmpty(map.get(FILES_WEIGHT.getKey())) ? map.get(FILES_WEIGHT.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(10, row, CollectionUtil.isNotEmpty(map.get(FILES_BMI.getKey())) ? map.get(FILES_BMI.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(11, row, CollectionUtil.isNotEmpty(map.get(ZL_JBXX_ZYH.getKey())) ? map.get(ZL_JBXX_ZYH.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(12, row, CollectionUtil.isNotEmpty(map.get(ZL_JBXX_YLFF.getKey())) ? map.get(ZL_JBXX_YLFF.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(13, row, CollectionUtil.isNotEmpty(map.get(ZL_JBXX_RYTJ.getKey())) ? map.get(ZL_JBXX_RYTJ.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(14, row, CollectionUtil.isNotEmpty(map.get(ZL_JBXX_ZYTIME.getKey())) ? map.get(ZL_JBXX_ZYTIME.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(15, row, CollectionUtil.isNotEmpty(map.get(ZL_JBXX_CYRQ.getKey())) ? map.get(ZL_JBXX_CYRQ.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(16, row, CollectionUtil.isNotEmpty(map.get(ZL_JBXX_ZYAGE.getKey())) ? map.get(ZL_JBXX_ZYAGE.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(17, row, CollectionUtil.isNotEmpty(map.get(ZL_JBXX_ZYTS.getKey())) ? map.get(ZL_JBXX_ZYTS.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(18, row, CollectionUtil.isNotEmpty(map.get(ZL_JBXX_LYFS.getKey())) ? map.get(ZL_JBXX_LYFS.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(19, row, CollectionUtil.isNotEmpty(map.get(ZL_JBXX_ZYZFY.getKey())) ? map.get(ZL_JBXX_ZYZFY.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(20, row, blsZlInfoVos.get(i).getZdmc());
//            excelWriter.writeCellValue(21, row, blsZlInfoVos.get(i).getZdbm());
//            excelWriter.writeCellValue(22, row, CollectionUtil.isNotEmpty(map.get(ZL_JBXX_DIE_T.getKey())) ? map.get(ZL_JBXX_DIE_T.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(23, row, CollectionUtil.isNotEmpty(map.get(ZL_JBXX_DIE_R.getKey())) ? map.get(ZL_JBXX_DIE_R.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(24, row, CollectionUtil.isNotEmpty(map.get(ZL_FBQK_FBSJ.getKey())) ? map.get(ZL_FBQK_FBSJ.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(25, row, CollectionUtil.isNotEmpty(map.get(ZL_FBQK_DY_TIME.getKey())) ? map.get(ZL_FBQK_DY_TIME.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(26, row, CollectionUtil.isNotEmpty(map.get(ZL_RYPG_SBP.getKey())) ? map.get(ZL_RYPG_SBP.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(27, row, CollectionUtil.isNotEmpty(map.get(ZL_RYPG_DBP.getKey())) ? map.get(ZL_RYPG_DBP.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(28, row, CollectionUtil.isNotEmpty(map.get(ZL_CYPG_CYYY.getKey())) ? map.get(ZL_CYPG_CYYY.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(29, row, CollectionUtil.isNotEmpty(map.get(ZL_NCX_MZ.getKey())) ? map.get(ZL_NCX_MZ.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(30, row, CollectionUtil.isNotEmpty(map.get(ZL_NCX_SS.getKey())) ? map.get(ZL_NCX_SS.getKey()).get(0).getAnswer() : "");
//            excelWriter.writeCellValue(31, row, CollectionUtil.isNotEmpty(map.get(ZL_NCX_T_SS.getKey())) ? map.get(ZL_NCX_T_SS.getKey()).get(0).getAnswer() : "");
//        }
//
//        String filename = StrUtil.format("zydn-{}.xlsx", DateUtil.date().toString("yyyyMMdd"));
//
//        //response为HttpServletResponse对象
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");
//        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
//        response.setHeader("Content-Disposition", "attachment;filename=" + filename);
//        ServletOutputStream out = null;
//        try {
//            out = response.getOutputStream();
//            excelWriter.flush(out);
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            // 关闭writer，释放内存
//            excelWriter.close();
//            //此处记得关闭输出Servlet流
//            IoUtil.close(out);
//        }
    }

    @Override
    public void zllx() {
        
    }

}
